package com.highgain.displacementtrend.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日期工具类
 * @author zhanglifeng
 * @date 2021/04/12 20:21
 */
public class DateUtil {
	private static final Logger log = LoggerFactory.getLogger(DateUtil.class);
	private static final Integer UNIX_TIME_LENGTH = 13;

	private static final String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日",
			"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd"};

	/**
	 * 将日期格式化为给定格式的字符串
	 */
	public static String formatDateToString(Date date, String pattern) {
		if (null == date) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 将字符串转换为日期
	 */
	public static final Date convertStringToDate(String pattern, String strDate){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			if(strDate != null && !"".equals(strDate)) {
				return sdf.parse(strDate);
			} else {
				return null;
			}
		} catch (ParseException e) {
			log.error("日期字符串解析异常",e);
			return null;
		}
	}

	/**
	 * 获取当前时间（精确到秒）
	 */
	public static String getThisSecondTime() {
		Calendar calendar = Calendar.getInstance();
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyMMddHHmmss");
		return formatter.format(currentTime);
	}

	/**
	 * 获取当前时间（精确到年月日时分秒）
	 */
	public static String getCurrentDayTime(){
		Calendar calendar = Calendar.getInstance();
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(currentTime);
	}

	/**
	 * 获取当前时间是0-上午还是1-下午
	 */
	public static int getCurrentMeridiem() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.AM_PM);
	}

	/**
	 * 获取当天时间
	 */
	public static String getCurrentDay() {
		Calendar calendar = Calendar.getInstance();
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(currentTime);
	}

	/**
	 * 获取昨天时间 字符串
	 */
	public static String getPreDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		Date preTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(preTime);
	}

	/**
	 * 获取间隔为n天的时间
	 */
	public static String getPreNDay(int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, n);
		Date preTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(preTime);
	}

	/**
	 * 获取当前日期的前6天
	 */
	public static String getCurrentBeforeWeek() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_WEEK, -6);
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(currentTime);
	}
	/**
	 * 设置开始时间为当天5点
	 */
	public static String getSpeedStartime() {
		StringBuffer str = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		str.append(formatter.format(currentTime));
		str.append(" 09:00");
		return str.toString();
	}

	/**
	 * 设置开始时间为当天5点
	 */
	public static String getSpeedEndime() {
		StringBuffer str = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		Date currentTime = calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		str.append(formatter.format(currentTime));
		str.append(" 17:00");
		return str.toString();
	}


	/**
	 * 获取当前月第一天 小时分钟为00:00:00
	 **/
	public static String getMonthFirstDay() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		str.append(sdf.format(lastDate.getTime()));
		str.append(" 00:00:00");
		return str.toString();
	}

	/**
	 * 获取当前月最后一天 小时分钟为23:59:59
	 **/
	public static String getMonthLastDay() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		lastDate.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
		str.append(sdf.format(lastDate.getTime()));
		str.append(" 23:59:59");
		return str.toString();
	}

	/**
	 * 获取当前年第一天 小时分钟为00:00:00
	 **/
	public static String getCurrentYearFirst() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
		str.append(sdf.format(cd.getTime()));
		str.append(" 00:00:00");
		return str.toString();
	}

	/**
	 * 获取当前年最后一天 小时分钟为23:59:59
	 **/
	public static String getCurrentYearLast() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
		cd.add(Calendar.YEAR, 1);
		cd.add(Calendar.DATE, -1);
		str.append(sdf.format(cd.getTime()));
		str.append(" 23:59:59");
		return str.toString();
	}

	/**
	 * 根据日期获取季度
	 */
	public static int getSeason() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH);
		return (month + 1 + 2) / 3;
	}

	/**
	 * 根据获取季度的第一天 小时分钟为00:00:00
	 */
	public static String getSeasonFirstDay(int i) {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if (i == 1) {
			i = 0;
		} else if (i == 2) {
			i = 3;
		} else if (i == 3) {
			i = 6;
		} else if (i == 4) {
			i = 9;
		}
		cal.set(Calendar.MONTH, i);// 设为季度开始月
		cal.set(Calendar.DATE, 1);
		str.append(sdf.format(cal.getTime()));
		str.append(" 00:00:00");
		return str.toString();
	}

	/**
	 * 根据获取季度的最后一天
	 */
	public static String getSeasonLastDay(int i) {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if (i == 1) {
			i = 2;
		} else if (i == 2) {
			i = 5;
		} else if (i == 3) {
			i = 8;
		} else if (i == 4) {
			i = 11;
		}
		cal.set(Calendar.MONTH, i);// 设为季度开始月
		cal.set(Calendar.DATE, 1);// 设季度开始月的第一天
		cal.add(Calendar.MONTH, 1);// 当前季度最后一月的下一月
		cal.add(Calendar.DATE, -1);// 当前季度最后一月的最后一天
		str.append(sdf.format(cal.getTime()));
		str.append(" 23:59:59");
		return str.toString();
	}

	/**
	 * 获取当前月
	 */
	public static String getMonth() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		String str = sdf.format(cal.getTime());
		return str;
	}

	/**
	 *获取当前年
	 */
	public static String getYear() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar cal = Calendar.getInstance();
		String str = sdf.format(cal.getTime());
		return str;
	}

	/**
	 * 获取当前周第一天 小时分钟为00:00:00
	 **/
	public static String getCurrentWeekFirst() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd");
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(new Date());
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday

		str.append(df.format(c.getTime()));
		str.append(" 00:00:00");

		return str.toString();
	}

	/**
	 * 获取当前周最后一天 小时分钟为23:59:59
	 **/
	public static String getCurrentWeekLast() {
		StringBuffer str = new StringBuffer();
		SimpleDateFormat df = new SimpleDateFormat(
				"yyyy-MM-dd");
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(new Date());
		// c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		str.append(df.format(c.getTime()));
		str.append(" 23:59:59");

		return str.toString();
	}

	/**
	 * 获取当前时间
	 */
	public static Date getNow() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	/**
	 * 获取当前日期（短）
	 */
	public static Date getNowDate() {
		Date now = getNow();
		return getDate(now);
	}
	/**
	 * 获取当前年份
	 */
	public static int getNowYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取当前月份
	 */
	public static int getNowMonth() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取昨天日期（短）
	 */
	public static Date getPreDate()
	{
		return getDate(addDay(getNow(), -1));
	}

	/**
	 * 获取日期（短）
	 */
	public static Date getDate(Date dateTime) {//时间工具修正月份获取
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);

		Calendar _cal = Calendar.getInstance();
		_cal.clear();
		_cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		_cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		_cal.set(Calendar.DATE, cal.get(Calendar.DATE));
		return _cal.getTime();
	}

	/**
	 * 获取时间所在的月份的最大天数
	 */
	public static int getMaxDayOfMonth(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取时间所在月份的第一天日期
	 */
	public static Date getFirstDateOfMonth(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	/**
	 * 获取时间所在月份的第最后一天日期
	 */
	public static Date getLastDateOfMonth(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	/**
	 * 获取时间所在月份的第几天
	 */
	public static int getDayOfMonth(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取时间的年份
	 */
	public static int getYear(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取时间的月份
	 */
	public static int getMonth(Date dateTime) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 添加天数
	 */
	public static Date addDay(Date dateTime, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 获取当年的第一天
	 */
	public static Date getCurrYearFirst(){
		Calendar currCal=Calendar.getInstance();
		int currentYear = currCal.get(Calendar.YEAR);
		return getYearFirst(currentYear);
	}

	/**
	 * 获取当年的最后一天
	 */
	public static Date getCurrYearLast()
	{
		String yearLastDay = DateUtil.getCurrentYearLast();
		return parseDateTime(yearLastDay);
	}

	/**
	 * 以今天为准，获取去年今天的日期
	 */
	public static Date getLastYearDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1);
		return calendar.getTime();
	}

	/**
	 * 获取某年第一天日期
	 */
	public static Date getYearFirst(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		return calendar.getTime();
	}

	/**
	 * 把日期类型字符串转换为yyyy-MM-dd类型日期
	 */
	public static Date parseDate(String dateValue) {
		if (dateValue == null) {
			return null;
		}

		String strFormat = "yyyy-MM-dd HH:mm:ss";

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;
		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		return newDate;
	}

	/**
	 * 把日期类型字符串转换为yyyy-MM-dd HH:mm:ss类型日期
	 */
	public static Date parseDateTime(String dateValue) {
		if (dateValue == null) {
			return null;
		}

		String strFormat = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;
		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			return null;
		}
		return newDate;
	}

	/**
	 * 返回当前日期的字符串形式：
	 */
	public static String getNowDateString(){
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now);
	}

	/**
	 * utc时间转成local时间
	 */
	public static Date utcToLocal(String utcTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		Date utcDate = null;
		try {
			utcDate = sdf.parse(utcTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		sdf.setTimeZone(TimeZone.getDefault());
		Date locatlDate = null;
		String localTime = sdf.format(utcDate.getTime());
		try {
			locatlDate = sdf.parse(localTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return locatlDate;
	}

	public static String UTCToCST(String UTCStr, String format) throws ParseException {
		String dateStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		Date date = sdf.parse(UTCStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
		dateStr = new SimpleDateFormat(format).format(calendar.getTime());
		return dateStr;
	}

	public static String timeStamp2Date(String time) throws ParseException {
		Long timeLong = Long.parseLong(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(sdf.format(timeLong));
		return sdf.format(date);
	}

	public static String isDateFormat(String string) {
		if(UNIX_TIME_LENGTH == string.length()){
			try {
				return timeStamp2Date(string);
			} catch (ParseException e) {
				//e.printStackTrace();
				return null;
			}
		}else{
			try {
				return UTCToCST(string, "yyyy-MM-dd hh:mm:ss");
			} catch (ParseException e) {
				//e.printStackTrace();
				return null;
			}
		}
	}

	public static void main(String[] args) {
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		System.out.println(dateFormat.length());
	}
}

